package com.hzw.net.manager

import com.hzw.net.vm.AbsRepository
import java.lang.IllegalArgumentException
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance

/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2020/12/15 17:50
 *
 * @Description:
 *
 */
object RepositoryManager {

    private val listThreadLocal =
        object : ThreadLocal<MutableMap<KClass<out AbsRepository>, AbsRepository>>() {
            override fun initialValue(): MutableMap<KClass<out AbsRepository>, AbsRepository>? {
                return mutableMapOf()
            }
        }

    fun addRepository(clazz: KClass<out AbsRepository>, repository: AbsRepository) {
        listThreadLocal.get()?.put(clazz, repository)
    }

    fun removeRepository(clazz: KClass<out AbsRepository>) {
        if (listThreadLocal.get()?.containsKey(clazz) == true) {
            listThreadLocal.get()?.remove(clazz)
        }
    }

    fun <T> getRepository(clazz: KClass<out AbsRepository>?): T {
        if (clazz == null) {
            throw IllegalArgumentException("获取AbsRepository的参数不能为 null ")
        }
        var repository = listThreadLocal.get()?.get(clazz)
        if (repository == null) {
            val temp = clazz.createInstance()
            listThreadLocal.get()?.put(clazz, temp)
            repository = temp
        }
        return repository as T
    }
}

inline fun <reified T : AbsRepository> getRepository(): T {
    return RepositoryManager.getRepository(T::class)
}